package com.yvon.im.processor;

import com.yvon.im.protocal.IMP;
import com.yvon.im.message.Message;
import io.netty.channel.Channel;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @author : wangyufeng
 * @since : 2021-12-02
 */
public class ProcessManager extends Processor<Message> {

    private Map<String, Processor> processorMap;

    public ProcessManager() {
        processorMap = new HashMap<>();
        processorMap.put(IMP.LOGIN.getName(), new LoginProcessor());
        processorMap.put(IMP.CHAT.getName(), new SendToUserProcessor());
    }


    @Override
    public void exec(Channel channel, String msg) {
        Message message = super.parse(msg);
        if (Objects.nonNull(message)) {
            String code = message.getCode();
            Processor processor = processorMap.get(code);
            processor.exec(channel, msg);
        }
    }

}
